-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
80 lines (67 loc) · 1.39 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <list>
using namespace std;
class HashTable
{
private:
list<int> *tab;
int tam;
int pos;
public:
HashTable(int tam)
{
this->tam = tam;
tab = new list<int>[tam];
}
void inserirValor(int valor)
{
this->pos = funcaoHash(valor);
tab[pos].push_back(valor);
}
void imprimirHash()
{
list<int>::iterator it;
for (int i = 0; i < tam; i++)
{
cout << i << " -> ";
list<int> hash = tab[i];
if (!hash.empty())
{
for (it = hash.begin(); it != hash.end(); it++)
{
if (next(it) != hash.end())
cout << *it << " -> ";
else
cout << *it << " -> \\";
}
cout << endl;
}
else
cout << "\\" << endl;
}
}
private:
int funcaoHash(int valor)
{
return valor % this->tam;
}
};
int main()
{
int n, m, c, valor;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> m >> c;
HashTable tab = HashTable(m);
for (int j = 0; j < c; j++)
{
cin >> valor;
tab.inserirValor(valor);
}
tab.imprimirHash();
if (i < n - 1)
cout << endl;
}
return 0;
}